using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.Profile;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using Commerce.Common;
using Commerce.Providers;

namespace CommerceForUmbraco.usercontrols
{
    public partial class MultiPageCheckout4 : System.Web.UI.UserControl
    {
        // the user profile - address, email, shipping, tax
        protected ProfileBase profile;

        // the current order - for shipping and tax calcs
        protected Order currentOrder = null;

        // the Umbraco Member Profile
        protected CommerceForUmbraco.profile.UmbracoProfile umbProfile;

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {  
                // get the order and set the order display
                SetCurrentOrder();
                SetProfile(currentOrder.UserName);

                phReceiptAddress.Controls.Add(new LiteralControl(receiptAddress()));
                //lblReceiptAddress.Text = receiptAddress();
                result.Text = currentOrder.OrderNumber.ToString();
                receiptDisplay.Text = currentOrder.mcOrderItemsTable();

            }
            catch (Exception ex)
            {
                message.Text = "We're sorry, an error occured.<br />" + ex.Message;
                umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Error, umbraco.BusinessLogic.User.GetUser(0), -1, String.Format("{0} -- {1}", ex.Message, ex.StackTrace));
            }

        }

        #region checkout utility methods

        /// <summary>
        /// Sets the current order based on value of "orderId" cookie
        /// </summary>
        public void SetCurrentOrder()
        {
            if (currentOrder == null)
            {
                int oid = -1;

                string orderId = HttpContext.Current.Request.Cookies["orderId"].Value;
                if (Int32.TryParse(orderId, out oid))
                {
                    if (oid >= 0)
                    {
                        currentOrder = OrderController.GetOrder(oid);
                    }
                }

            }
        }

        /// <summary>
        /// Sets the profile to the current user and initializes ProfileBase
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        protected void SetProfile(string userName)
        {
            // set the Umbraco profile
            umbProfile = new CommerceForUmbraco.profile.UmbracoProfile(userName);
        }

        /// <summary>
        /// Formats the address in XHTML table for display
        /// </summary>
        /// <returns></returns>
        protected string receiptAddress()
        {
            StringBuilder sb = new StringBuilder();

            if (currentOrder != null)
            {
                sb.Append("<table><tr><th>Shipping Address:</th><th>Billing Address:</th></tr><tr><td>");
                sb.Append(currentOrder.ShipToAddress);
                sb.Append("</td><td>");
                sb.Append(currentOrder.BillToAddress);
                sb.Append("</td></tr></table>");
            }

            return sb.ToString();
        }

    #endregion

    }
}